home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0084_Allow word wrap in TStringGrid.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  5.3 KB  |  168 lines

  1. {
  2. I have finally created a custom component, TWrapGrid that allows you to
  3. use a TStringGrid, but also wrap the text in a cell.
  4. This is the beta version, so I encourage you to experiment with it,
  5. try it out, and send me comments on what you think of it.
  6. When you use it, remember to se the RowHeights (or DefaultRowHeight)
  7. large enough so that when it wraps, it shows up in the cell.
  8.  
  9. To install, copy the following text and paste it into a Unit.  Save it
  10. under the name 'Wrapgrid.PAS'. Then follow the directions I put in the
  11. header of the component.
  12.  
  13. Also, I am looking for a Web page where I can put this on for people to
  14. download.
  15.  
  16. I'm also looking for feedback on this component, so please try it and tell me
  17. what you think.
  18.  
  19. Here is the code!
  20. -------------------------------------------
  21. {  This is a custom component for Delphi.
  22.    It is wraps text in a TStringGrid, thus the name TWrapGrid.
  23.    It was created by Luis J. de la Rosa.
  24.    E-mail: delarosa@ix.netcom.com
  25.    Everyone is free to use it, distribute it, and enhance it.
  26.  
  27.    To use:  Go to the 'Options' - 'Install Components' menu selection in Delphi.
  28.             Select 'Add'.
  29.             Browse for this file, which will be named 'Wrapgrid.PAS'.
  30.             Select 'OK'.
  31.             You have now added this to the Samples part of your component
  32.                palette.
  33.             After that, you can use it just like a TStringGrid.
  34.  
  35.    Please send any questions or comments to delarosa@ix.netcom.com
  36.    Enjoy!
  37.  
  38.    A few additional programming notes:
  39.    I have overridden the Create and DrawCell methods.  Everything else should
  40.    behave just like a TStringGrid.
  41.    The Create sets the DefaultDrawing to False, so you don't need to.
  42.  
  43.    Also, I am using the pure block emulation style of programming, making my
  44.    code easier to read.
  45. }
  46.    
  47. unit Wrapgrid;
  48.  
  49. interface
  50.  
  51. uses
  52.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  53.   Forms, Dialogs, Grids;
  54.  
  55. type
  56.   TWrapGrid = class(TStringGrid)
  57.   private
  58.     { Private declarations }
  59.   protected
  60.     { Protected declarations }
  61.     { This DrawCell procedure wraps text in the grid cell }
  62.     procedure DrawCell(ACol, ARow : Longint; ARect : TRect;
  63.       AState : TGridDrawState); override;
  64.   public
  65.     { Public declarations }
  66.     { The Create procedure is overriden to use the DrawCell procedure by
  67.          default }
  68.     constructor Create(AOwner : TComponent); override;
  69.   published
  70.     { Published declarations }
  71.   end;
  72.  
  73. procedure Register;
  74.  
  75. implementation
  76.  
  77.  
  78. constructor TWrapGrid.Create(AOwner : TComponent);
  79. begin
  80.    { Create a TStringGrid }
  81.    inherited Create(AOwner);
  82.  
  83.    { Make the drawing use our DrawCell procedure by default }
  84.    DefaultDrawing := FALSE;
  85. end;
  86.  
  87.  
  88.  
  89. { This DrawCell procedure wraps text in the grid cell }
  90. procedure TWrapGrid.DrawCell(ACol, ARow : Longint; ARect : TRect;
  91.    AState : TGridDrawState);
  92. var
  93.    Sentence,                  { What is left in the cell to output }
  94.    CurWord : String;          { The word we are currently outputting }
  95.    SpacePos,                  { The position of the first space }
  96.    CurX,                      { The x position of the 'cursor' }
  97.    CurY : Integer;            { The y position of the 'cursor' }
  98.    EndOfSentence : Boolean;   { Whether or not we are done outputting the cell }
  99. begin
  100.    { Initialize the font to be the control's font }
  101.    Canvas.Font := Font;
  102.  
  103.    with Canvas do begin
  104.       { If this is a fixed cell, then use the fixed color }
  105.       if gdFixed in AState then begin
  106.          Pen.Color   := FixedColor;
  107.          Brush.Color := FixedColor;
  108.       end
  109.       { else, use the normal color }
  110.       else begin
  111.          Pen.Color   := Color;
  112.          Brush.Color := Color;
  113.       end;
  114.  
  115.       { Prepaint cell in cell color }
  116.       Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
  117.    end;
  118.  
  119.    { Start the drawing in the upper left corner of the cell }
  120.    CurX := ARect.Left;
  121.    CurY := ARect.Top;
  122.  
  123.    { Here we get the contents of the cell }
  124.    Sentence := Cells[ACol, ARow];
  125.  
  126.    { for each word in the cell }
  127.    EndOfSentence := FALSE;
  128.    while (not EndOfSentence) do begin
  129.       { to get the next word, we search for a space }
  130.       SpacePos := Pos(' ', Sentence);
  131.       if SpacePos > 0 then begin
  132.          { get the current word plus the space }
  133.          CurWord := Copy(Sentence, 0, SpacePos);
  134.  
  135.          { get the rest of the sentence }
  136.          Sentence := Copy(Sentence, SpacePos + 1, Length(Sentence) - SpacePos);
  137.       end
  138.       else begin
  139.          { this is the last word in the sentence }
  140.          EndOfSentence := TRUE;
  141.          CurWord := Sentence;
  142.       end;
  143.  
  144.       with Canvas do begin
  145.          { if the text goes outside the boundary of the cell }
  146.          if (TextWidth(CurWord) + CurX) > ARect.Right then begin
  147.             { wrap to the next line }
  148.             CurY := CurY + TextHeight(CurWord);
  149.             CurX := ARect.Left;
  150.          end;
  151.  
  152.          { write out the word }
  153.          TextOut(CurX, CurY, CurWord);
  154.          { increment the x position of the cursor }
  155.          CurX := CurX + TextWidth(CurWord);
  156.       end;
  157.    end;
  158. end;
  159.  
  160. procedure Register;
  161. begin
  162.    { You can change Samples to whichever part of the Component Palette you want
  163.      to install this component to }
  164.    RegisterComponents('Samples', [TWrapGrid]);
  165. end;
  166.  
  167. end.
  168.